/*
 * inAccountManger.java
 *
 * Created on __DATE__, __TIME__
 */

package hotel.view;

import hotel.dao.YuDingDao;
import hotel.dao.ShouYinDao;
import hotel.model.ShouYin;
import hotel.util.DbUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.sun.org.apache.bcel.internal.generic.NEW;
import java.awt.Font;
import javax.swing.ImageIcon;

public class inAccountManger extends javax.swing.JInternalFrame {

	DbUtil dbUtil = new DbUtil();
	ShouYinDao cashierDao = new ShouYinDao();


	public inAccountManger() {
		initComponents();
		this.fillTable();
		this.setLocation(200, 50);//设置窗口位置
	}
	public void fillTable() {
		DefaultTableModel dtm = (DefaultTableModel) inAccountTable.getModel();
		dtm.setRowCount(0);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet re = cashierDao.cashier(con);
			while (re.next()) {
				Vector v = new Vector();
				//添加每一行的数据
				v.add(re.getString("id"));
				v.add(re.getString("type"));
				v.add(re.getString("money"));
				v.add(re.getString("remarks"));
				v.add(re.getString("time"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		inAccountTable = new javax.swing.JTable();
		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();

		jLabel1.setFont(new Font("宋体", Font.PLAIN, 22));
		money = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		remarks = new javax.swing.JTextField();
		add = new javax.swing.JButton();
	
		add.setFont(new Font("宋体", Font.PLAIN, 22));
		jLabel3 = new javax.swing.JLabel();
		jLabel3.setFont(new Font("宋体", Font.PLAIN, 22));

		type = new javax.swing.JTextField();

		setClosable(true);
		setIconifiable(true);
		setTitle("入账管理");

		inAccountTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { null, null, null, null, null },
						{ null, null, null, null, null },
						{ null, null, null, null, null },
						{ null, null, null, null, null } }, new String[] {
						"编号", "状态", "金额", "备注", "时间" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false,
					false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		jScrollPane1.setViewportView(inAccountTable);

		jLabel1.setText("金额");

		jLabel2.setText("金额");

		add.setText("添加");
		add.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				addActionPerformed(evt);
			}
		});

		jLabel3.setText("类型");

		type.setEditable(false);
		type.setText("收入");
		type.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jTextField1ActionPerformed(evt);
			}
		});

		org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jPanel1Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.TRAILING)
														.add(add)
														.add(
																jPanel1Layout
																		.createSequentialGroup()
																		.add(
																				jPanel1Layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.TRAILING,
																								false)
																						.add(
																								org.jdesktop.layout.GroupLayout.LEADING,
																								jPanel1Layout
																										.createSequentialGroup()
																										.add(
																												jLabel3)
																										.addPreferredGap(
																												org.jdesktop.layout.LayoutStyle.RELATED)
																										.add(
																												type))
																						.add(
																								jPanel1Layout
																										.createSequentialGroup()
																										.add(
																												jLabel1)
																										.addPreferredGap(
																												org.jdesktop.layout.LayoutStyle.RELATED)
																										.add(
																												money,
																												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																												176,
																												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
																		.add(
																				18,
																				18,
																				18)
																		.add(
																				jLabel2)
																		.add(
																				18,
																				18,
																				18)
																		.add(
																				remarks,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				181,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap(439, Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												jPanel1Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel1)
														.add(
																money,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(jLabel2)
														.add(
																remarks,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.add(18, 18, 18)
										.add(
												jPanel1Layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel3)
														.add(
																type,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE).add(add)));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				org.jdesktop.layout.GroupLayout.TRAILING,
				layout.createSequentialGroup().addContainerGap().add(
						layout.createParallelGroup(
								org.jdesktop.layout.GroupLayout.TRAILING).add(
								org.jdesktop.layout.GroupLayout.LEADING,
								jPanel1,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).add(
								org.jdesktop.layout.GroupLayout.LEADING,
								jScrollPane1,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								897, Short.MAX_VALUE)).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				org.jdesktop.layout.GroupLayout.LEADING).add(
				layout.createSequentialGroup().addContainerGap().add(
						jScrollPane1,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 131,
						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.UNRELATED).add(
								jPanel1,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(30, Short.MAX_VALUE)));

		pack();
	}
	private void addActionPerformed(java.awt.event.ActionEvent evt) {
		String money = this.money.getText();
		String remarks = this.remarks.getText();
		String type = this.type.getText();
		if("".equals(money) || money == null){
			JOptionPane.showMessageDialog(null, "请输入金额");
			return;
		}
		if("".equals(remarks) || remarks == null){
			JOptionPane.showMessageDialog(null, "请输入备注");
			return;
		}
		ShouYin cashier = new ShouYin();
		cashier.setMoney(money);
		cashier.setRemarks(remarks);
		cashier.setType(type);
		Connection con  = null;	
		try {
			con = dbUtil.getCon();
			int result = cashierDao.addCashier(con, cashier);
			if(result != 0){
				JOptionPane.showMessageDialog(null, "添加成功!");
				this.fillTable();
				this.money.setText("");
				this.remarks.setText("");
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "添加失败!");
			e.printStackTrace();
		}
	}

	private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
	}
	private javax.swing.JButton add;
	private javax.swing.JTable inAccountTable;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextField type;
	private javax.swing.JTextField money;
	private javax.swing.JTextField remarks;

}